package org.example.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.repository.IStrategyRepository;
import org.example.domain.strategy.service.rule.chain.AbstractLogicChain;
import org.example.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.example.types.common.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component("rule_blacklist")
public class BlackLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository repository;
    @Override
    protected String ruleModel() {
        return DefaultChainFactory.LogicModel.RULE_BLACKLIST.getCode();
    }

    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {
        //100：user001,user002,user003
        log.info("责任链处理-黑名单开始 userId:{} strategyId :{}  ruleModel: {}",userId,strategyId, ruleModel());
        String ruleValue = repository.queryStrategyRuleValue(strategyId, ruleModel());
        String[] splitRuleValue = ruleValue.split(Constants.COLON);  //":"拆
        Integer awardId=Integer.parseInt(splitRuleValue[0]);

        //100：user001,user002,user003
        //过滤其他的规则
        String[] userBlackIds = splitRuleValue[1].split(Constants.SPLIT);
        for (String userBlackId :userBlackIds) {
            if(userId.equals(userBlackId)){
                log.info("责任链处理-黑名接管 userId:{} strategyId :{}  ruleModel: {} awardId: {}",userId,strategyId,ruleModel(),awardId);
                return DefaultChainFactory.StrategyAwardVO.builder()
                        .awardId(awardId)
                        .logicModel(ruleModel())
                        .awardRuleValue("0.01,1") //默认写死0.01-1积分
                        .build();
            }
        }

        //不是黑名单走后面逻辑
        log.info("责任链处理-黑名单放行 userId:{}  strategyId: {}  ruleModel: {}", userId,strategyId,ruleModel());
        return next().logic(userId,strategyId);
    }




}
